package com.atguigu.cms.controller;


import com.atguigu.cms.entity.CrmBanner;
import com.atguigu.cms.service.CrmBannerService;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exceptionhandler.SelfExceptionHandler;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 针对后端管理人员的控制器
 * crud
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-03-30
 */
@CrossOrigin
@RestController
@RequestMapping("/cms/crmbanner")
public class AdminBannerController {

    @Autowired
    private CrmBannerService bannerService;


    //1 分页查询banner
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page,@PathVariable long limit) {
        Page<CrmBanner> pageBanner = new Page<>(page,limit);
        bannerService.page(pageBanner,null);
        return R.ok().data("items",pageBanner.getRecords()).data("total",pageBanner.getTotal());
    }

    /**
     * 后端人员添加banner
     * @param crmBanner
     * @return
     */
    @PostMapping("/addBanner")
    public R  addBanner(@RequestBody CrmBanner crmBanner)
    {
        if(crmBanner==null)
        {
            throw new SelfExceptionHandler("添加banner失败",20001);
        }

        boolean b = bannerService.save(crmBanner);
        if(b==false)
        {
            throw new SelfExceptionHandler("添加banner失败",20001);
        }

        return R.ok();
    }

    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public R get(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("item", banner);
    }
    /**
     * 获取所有的数据
     * @return
     */
   @GetMapping("/getBanner")
    public R getBanner()
   {

       List<CrmBanner> list = bannerService.list(null);
       return R.ok().data("bannerList",list);

   }

    /**
     * 修改
     * @param banner
     * @return
     */
   @PutMapping("/updateBanner")
   public R updateBanner(@RequestBody CrmBanner banner)
   {

       bannerService.updateById(banner);

       return  R.ok();

   }

    /**
     * 删除
     * @param bannerId
     * @return
     */
   @DeleteMapping("/deleteBanner/{bannerId}")
   public R deleteBanner(@PathVariable String bannerId)
   {
       bannerService.removeById(bannerId);
       return R.ok();
   }

















}

